package com.jagochan.module.springai.controller;

import com.jagochan.module.springai.common.ChatTypeConstants;
import com.jagochan.module.springai.common.repository.ChatHistoryRepository;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RequestMapping("/ai")
@RestController
public class CustomerServiceController {

    private final ChatClient serviceChatClient;
    private final ChatHistoryRepository chatHistoryRepository;

    @ApiOperation(value = "智能客服")
    @RequestMapping(value = "/service")
    public Flux<String> service(@ApiParam("提示词") String prompt, @ApiParam("聊天ID") String chatId) {
        // 保存聊天ID
        chatHistoryRepository.save(ChatTypeConstants.SERVICE, chatId);
        // 请求模型
        return serviceChatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();
    }

}
